package com.project.server.estates;

import com.project.common.annotation.Log;
import com.project.common.core.controller.BaseController;
import com.project.common.core.domain.AjaxResult;
import com.project.common.core.domain.model.LoginUser;
import com.project.common.core.page.TableDataInfo;
import com.project.common.enums.BusinessType;
import com.project.common.utils.ServletUtils;
import com.project.common.utils.StringUtils;
import com.project.common.utils.poi.ExcelUtil;
import com.project.framework.web.service.TokenService;
import com.project.server.domain.estates.TPayRecord;
import com.project.server.service.companies.ITCompanyService;
import com.project.server.service.estates.ITPayRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.project.server.pojo.estates.PayRecordPojo;

import javax.annotation.Resource;
import java.util.List;

/**
 * 缴费记录Controller
 *
 * @author project
 * @date 2021-06-28
 */
@RestController
@RequestMapping("/server/payRecord")
public class TPayRecordController extends BaseController {

    @Resource
    private ITPayRecordService tPayRecordService;

    @Autowired
    private TokenService tokenService;

    @Resource
    private ITCompanyService companyService;

    /**
     * 查询缴费记录列表
     */
    @PreAuthorize("@ss.hasPermi('server:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(TPayRecord tPayRecord) {
        startPage();
        List<TPayRecord> list = tPayRecordService.selectTPayRecordList(tPayRecord);
        return getDataTable(list);
    }

    /**
     * 导出缴费记录列表
     */
    @PreAuthorize("@ss.hasPermi('server:record:export')")
    @Log(title = "缴费记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TPayRecord tPayRecord) {
        List<TPayRecord> list = tPayRecordService.selectTPayRecordList(tPayRecord);
        ExcelUtil<TPayRecord> util = new ExcelUtil<TPayRecord>(TPayRecord.class);
        return util.exportExcel(list, "缴费记录列表");
    }

    /**
     * 导入缴费记录
     */
    @Log(title = "缴费记录", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('server:record:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport,@RequestParam(value = "ruleType",required = false) String ruleType) throws Exception{
        ExcelUtil<TPayRecord> util = new ExcelUtil<TPayRecord>(TPayRecord.class);
        List<TPayRecord> tPayRecordList = util.importExcel(file.getInputStream());
        for (int i=0 ;i<tPayRecordList.size();i++){
            tPayRecordList.get(i).setRuleType(ruleType);
        }
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = tPayRecordService.importPayRecord(tPayRecordList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 导入缴费记录模板
     */
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate(){
        ExcelUtil<PayRecordPojo> util = new ExcelUtil<PayRecordPojo>(PayRecordPojo.class);
        return util.importTemplateExcel("缴费记录模板");
    }

    /**
     * 获取缴费记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('server:record:query')")
    @GetMapping(value = {"/","/{payId}"})
    public AjaxResult getInfo(@PathVariable(value = "payId",required = false) Long payId) {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("comps",companyService.selectTCompanyAll());
        if (StringUtils.isNotNull(payId)){
            ajax.put(AjaxResult.DATA_TAG,tPayRecordService.selectTPayRecordById(payId));
            ajax.put("compId",companyService.selectTCompanyByPayId(payId));
        }
        return ajax;
    }

    /**
     * 新增缴费记录
     */
    @PreAuthorize("@ss.hasPermi('server:record:add')")
    @Log(title = "缴费记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody TPayRecord tPayRecord) {
        return toAjax(tPayRecordService.insertTPayRecord(tPayRecord));
    }

    /**
     * 修改缴费记录
     */
    @PreAuthorize("@ss.hasPermi('server:record:edit')")
    @Log(title = "缴费记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody TPayRecord tPayRecord) {
        return toAjax(tPayRecordService.updateTPayRecord(tPayRecord));
    }

    /**
     * 删除缴费记录
     */
    @PreAuthorize("@ss.hasPermi('server:record:remove')")
    @Log(title = "缴费记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{payIds}")
    public AjaxResult remove(@PathVariable Long[] payIds) {
        return toAjax(tPayRecordService.deleteTPayRecordByIds(payIds));
    }
}
